<?php
/**
 * <pre>
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT      : rubik-base
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * [GENERAL INFO OF THIS FILE] 
 *
 * </pre>
 * 
 * @version 		1.0
 * @author			Dang Nhon Hoa <hoadn@rubik.com.vn>
 * @access			public
 * @package			rubik.base.html
 *
 * Created on	: May 25, 2008
 */
RExtImporter::cls('ajaxfileupload.AjaxHtmlUploadControl');

class AjaxVideoHtmlUploadControl extends AjaxHtmlUploadControl {
	
	var $_width = null;
	var $_height = null;
	
	function setSize($width, $height) {
		$this->_width = $width;
		$this->_height = $height;
	}
	
	function getPreviewHtml() {

		$idPlayer = $this->_inputName."_flashPlayer";
		
		$htmlPlayer = $this->_getPlayerHtml('__VIDEO_LINK__');
		$htmlPlayer = str_replace("\r", " ", $htmlPlayer);
		$htmlPlayer = str_replace("\n", " ", $htmlPlayer);
		
		$htmlPlayer = addslashes($htmlPlayer);
		$js = '
		var playerHtml = "'.$htmlPlayer.'";
		function processSuccessVideo(data, id) {
			if (data.type != "video") {
				return;
			}
			
			id = id.replace("_id", "_flashPlayer");
			var html = playerHtml.replace("__VIDEO_LINK__", SYS_ROOT_URL + data.file);
			html = html.replace("__VIDEO_LINK__", SYS_ROOT_URL + data.file);
			$(id).html(html);
		}
		';
		SysResponse::addJavascriptContent($js);
		
		$videoLink = RubikHelper::checkAndAddURL($this->getValueWithoutUrl());
		$result = '<div id="'.$idPlayer.'">'.
					 $this->_getPlayerHtml($videoLink)
				 .'</div>';
		
		return $result;
	}
	
	function _getPlayerHtml($videoLink) {
		$swfFile = 'player.swf';
		$flashVar = 'file='.$videoLink.'&bgcolor1=189ca8&bgcolor2=085c68';
		
		$swfFile	= SYS_ROOT_URL."libraries/player/video/{$swfFile}";
		
		$result = RHtmlHelper::createFlashObject($swfFile, $this->_width, $this->_height, 'html', array('FlashVars'=>$flashVar, 'bgcolor'=>'000000', 'name'=>$this->_inputName."_player"));
		return $result;
	}
	
	/**
	 * Static function
	 * @param $valueOrArrayContain
	 * @return unknown_type
	 */
	function getAjaxFileResult($valueOrArrayContain) {
		$pathName = 'path_'.$this->_inputName;
		$path = $valueOrArrayContain[$pathName];
		if ($path) {
			$path = base64_decode($path);
			$this->_pathFromRoot = $path;
		}
		
		$this->setValue($valueOrArrayContain);
		if ($this->hasError()) {
			$result['error'] = $this->getError();
		}
		else {
			$result['error'] = '';
			$result['type'] = 'video';
			$result['file'] = $this->getValueWithoutUrl();
			$result['msg'] = 'Upload successfull';
		}
		
		return json_encode($result);
	}
}
?>